package com.jwgf.smarowindingmachine_unity.ui;

import java.util.ArrayList;
import java.util.Calendar;

import com.jwgf.report.DayInfo;
import com.jwgf.report.ShiftInfo;
import com.jwgf.report.ShiftTimeRule;
import com.jwgf.smarowindingmachine_unity.R;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class EditClassSettingDialog extends Dialog implements
        android.view.View.OnClickListener {

    private Context ctx;
    private ShiftTimeRule stRule = ShiftTimeRule.getInstance();
    private static final int SET_FAIL = 9999;
    private int weekIndex = 0;
    private int indexInCalender = Calendar.MONDAY;
    private String[] strWeekDay;
    private static final int DEFAULT_CLASSES_TOTAL = 4;
    private TextView tv_start_time[] = new TextView[DEFAULT_CLASSES_TOTAL];
    private TextView tv_class[] = new TextView[DEFAULT_CLASSES_TOTAL];
    private ImageButton[] bt_class_select = new ImageButton[DEFAULT_CLASSES_TOTAL];
    private RelativeLayout[] rl_start_time_select = new RelativeLayout[DEFAULT_CLASSES_TOTAL];
    private Button btSmartSchedule;
    private Button btDaySchedule;
    private Button btCancelSchedule;
    private TextView tvWeekNum;

    private ProductionManagementCustomSelectClassNumberDialog classNumberSelectDialog; // select
    // class
    // number
    // dialog
    private CustomTimeDialog startTimeSetDialog; // set start time dialog

    public EditClassSettingDialog(Context context, int position) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.fragment_production_management_switch_class_edit_monday);
        ctx = context;
        weekIndex = position;
        initPage();

    }

    private void initPage() {
        // 班次
        tv_class[0] = (TextView) findViewById(R.id.class_one_value);
        tv_class[1] = (TextView) findViewById(R.id.class_two_value);
        tv_class[2] = (TextView) findViewById(R.id.class_three_value);
        tv_class[3] = (TextView) findViewById(R.id.class_four_value);
        // 开始时间
        tv_start_time[0] = (TextView) findViewById(R.id.start_time_one);
        tv_start_time[1] = (TextView) findViewById(R.id.start_time_two);
        tv_start_time[2] = (TextView) findViewById(R.id.start_time_three);
        tv_start_time[3] = (TextView) findViewById(R.id.start_time_four);
        // 班次按钮
        bt_class_select[0] = (ImageButton) findViewById(R.id.class_one_btn);
        bt_class_select[1] = (ImageButton) findViewById(R.id.class_two_btn);
        bt_class_select[2] = (ImageButton) findViewById(R.id.class_three_btn);
        bt_class_select[3] = (ImageButton) findViewById(R.id.class_four_btn);
        // 时间选择
        rl_start_time_select[0] = (RelativeLayout) findViewById(R.id.start_time_one_layout);
        rl_start_time_select[1] = (RelativeLayout) findViewById(R.id.start_time_two_layout);
        rl_start_time_select[2] = (RelativeLayout) findViewById(R.id.start_time_three_layout);
        rl_start_time_select[3] = (RelativeLayout) findViewById(R.id.start_time_four_layout);

        for (int i = 0; i < DEFAULT_CLASSES_TOTAL; i++) {
            bt_class_select[i].setOnClickListener(this);
            rl_start_time_select[i].setOnClickListener(this);
        }

        // 换班按钮
        if (0 == weekIndex) {
            btSmartSchedule = (Button) findViewById(R.id.smart_schedule);
            btSmartSchedule.setOnClickListener(this);
            btSmartSchedule.setVisibility(View.VISIBLE);
        }
        btDaySchedule = (Button) findViewById(R.id.day_shift);
        btDaySchedule.setOnClickListener(this);
        btCancelSchedule = (Button) findViewById(R.id.schedule_cancel);
        btCancelSchedule.setOnClickListener(this);

        // 星期
        tvWeekNum = (TextView) findViewById(R.id.switch_class_week);
        strWeekDay = ctx.getResources().getStringArray(R.array.report_weekday);
        tvWeekNum.setText(strWeekDay[weekIndex]);

        // 初始化班次和开始时刻
        indexInCalender = (weekIndex + 1) % 7 + 1;
        ArrayList<ShiftInfo> infoList = ShiftTimeRule.getInstance()
                .getDayInfo(indexInCalender).getAllShiftInfo();
        for (int i = 0; i < DEFAULT_CLASSES_TOTAL; i++) {
            tv_class[i].setText(String.valueOf(infoList.get(i).getNum()));
            if (0 == infoList.get(i).getNum()) {
                tv_start_time[i].setText("--:--");

            } else {
                tv_start_time[i].setText((infoList.get(i).getHour() < 10 ? ("0" + infoList.get(i).getHour()) : infoList.get(i).getHour())
                        + ":" + (infoList.get(i).getMinute() < 10 ? ("0" + infoList.get(i).getMinute())
                        : infoList.get(i).getMinute()));

            }
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // set class number
            case R.id.class_one_btn: {
                displaySetClassNumberDialog(0);

            }
            break;
            case R.id.class_two_btn: {
                displaySetClassNumberDialog(1);
            }
            break;
            case R.id.class_three_btn: {
                displaySetClassNumberDialog(2);
            }
            break;
            case R.id.class_four_btn: {
                displaySetClassNumberDialog(3);
            }
            break;

            //
            // set class start_time
            //
            case R.id.start_time_one_layout: {
                displayPickTimeDialog(0);
            }
            break;
            case R.id.start_time_two_layout: {
                displayPickTimeDialog(1);
            }
            break;
            case R.id.start_time_three_layout: {
                displayPickTimeDialog(2);
            }
            break;
            case R.id.start_time_four_layout: {
                displayPickTimeDialog(3);
            }
            break;

            case R.id.smart_schedule: {/* smart schedule button on Monday */
                ShiftInfo[] shiftInfoArray = new ShiftInfo[4];
                DayInfo dayInfo = new DayInfo();
                for (int i = 0; i < DEFAULT_CLASSES_TOTAL; i++) {
                    if (!tv_class[i].getText().toString().equals("0")) {
                        String time[] = tv_start_time[i].getText().toString()
                                .split(":");
                        int hour = Integer.valueOf(time[0]);
                        int minute = Integer.valueOf(time[1]);
                        shiftInfoArray[i] = new ShiftInfo(
                                Integer.valueOf(tv_class[i].getText().toString()),
                                hour, minute);
                        dayInfo.setShiftInfo(i, shiftInfoArray[i]);
                    } else {
                        shiftInfoArray[i] = new ShiftInfo(0, 0, 0);
                        dayInfo.setShiftInfo(i, shiftInfoArray[i]);
                    }
                }
                if (stRule.smartSetting(dayInfo)) {
                    WarningDialog.show(
                            ctx,
                            ctx.getString(R.string.string_pm_production_set_switch_class_smart_schedule_message));
                    this.dismiss();
                } else {
                    WarningDialog dia = new WarningDialog();
                    WarningDialog.show(ctx,
                            ctx.getString(R.string.warning_out_24Hour_condition));
                }

                break;
            }
            case R.id.day_shift:/* day schedule every day */ {
                ShiftInfo[] shiftInfoArray = new ShiftInfo[4];
                DayInfo dayInfo = new DayInfo();
                for (int i = 0; i < DEFAULT_CLASSES_TOTAL; i++) {
                    if (!tv_class[i].getText().toString().equals("0")) {
                        String time[] = tv_start_time[i].getText().toString()
                                .split(":");
                        int hour = Integer.valueOf(time[0]);
                        int minute = Integer.valueOf(time[1]);
                        shiftInfoArray[i] = new ShiftInfo(
                                Integer.valueOf(tv_class[i].getText().toString()),
                                hour, minute);
                        dayInfo.setShiftInfo(i, shiftInfoArray[i]);
                    } else {
                        shiftInfoArray[i] = new ShiftInfo(0, 0, 0);
                        dayInfo.setShiftInfo(i, shiftInfoArray[i]);
                    }
                }

                if (stRule.normalSetting(indexInCalender, dayInfo)) {// 时间表符合规则
                    WarningDialog dia = new WarningDialog();
                    WarningDialog.show(
                            ctx,
                            ctx.getString(R.string.string_pm_production_set_switch_class_day_shift_message));
                    this.dismiss();
                } else {
                    WarningDialog dia = new WarningDialog();
                    WarningDialog.show(ctx,
                            ctx.getString(R.string.warning_out_24Hour_condition));
                }

                break;
            }
            case R.id.schedule_cancel:/* press cancel button at edit class page */ {
                this.dismiss();
            }
            break;

            default:
                break;
        }
    }

    private void displayPickTimeDialog(final int indexClassTimeNum) {

        final Calendar calendar = Calendar.getInstance();

        startTimeSetDialog = new CustomTimeDialog(ctx);
        startTimeSetDialog.show();
        Button btn_close = (Button) startTimeSetDialog
                .findViewById(R.id.btn_close);
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                startTimeSetDialog.dismiss();
            }
        });

        String time[] = tv_start_time[indexClassTimeNum].getText().toString()
                .split(":");
        String hourTime = time[0];
        String minuteTime = time[1];

        final TextView hour = (TextView) startTimeSetDialog
                .findViewById(R.id.hour);
//		hour.setText(String.valueOf(calendar.get(Calendar.HOUR_OF_DAY)));
        hour.setText(hourTime);
        final TextView min = (TextView) startTimeSetDialog
                .findViewById(R.id.min);
//		min.setText(String.valueOf(calendar.get(Calendar.MINUTE)));
        min.setText(minuteTime);
        Button btn_hour_up = (Button) startTimeSetDialog
                .findViewById(R.id.btn_hour_up);
        btn_hour_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                int num_hour = Integer.valueOf(hour.getText().toString());
                num_hour++;
                if (num_hour > 23)
                    num_hour = 0;
                hour.setText(String.valueOf(num_hour));
            }
        });
        Button btn_hour_down = (Button) startTimeSetDialog
                .findViewById(R.id.btn_hour_down);
        btn_hour_down.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int num_hour = Integer.valueOf(hour.getText().toString());
                num_hour--;
                if (num_hour < 0)
                    num_hour = 23;
                hour.setText(String.valueOf(num_hour));
            }
        });
        Button btn_min_up = (Button) startTimeSetDialog
                .findViewById(R.id.btn_min_up);
        btn_min_up.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int num_min = Integer.valueOf(min.getText().toString());
                num_min++;
                if (num_min > 59)
                    num_min = 0;
                min.setText(String.valueOf(num_min));
            }

        });
        Button btn_min_down = (Button) startTimeSetDialog
                .findViewById(R.id.btn_min_down);
        btn_min_down.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int num_min = Integer.valueOf(min.getText().toString());
                num_min--;
                if (num_min < 0)
                    num_min = 59;
                min.setText(String.valueOf(num_min));
            }

        });
        Button btn_save = (Button) startTimeSetDialog
                .findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                int num_hour = Integer.valueOf(hour.getText().toString());
                int num_min = Integer.valueOf(min.getText().toString());

                Calendar c = Calendar.getInstance();
                c.set(Calendar.HOUR_OF_DAY, num_hour);
                c.set(Calendar.MINUTE, num_min);
                c.set(Calendar.SECOND, 0);
                c.set(Calendar.MILLISECOND, 0);
                long when = c.getTimeInMillis();
                StringBuilder builder = new StringBuilder();
                if (String.valueOf(num_hour).length() == 1)
                    builder.append("0");
                builder.append(String.valueOf(num_hour));
                builder.append(":");
                if (String.valueOf(num_min).length() == 1)
                    builder.append("0");
                builder.append(String.valueOf(num_min));

                if (tv_start_time[indexClassTimeNum] != null) {
                    tv_start_time[indexClassTimeNum].setText(builder.toString());
                }
                startTimeSetDialog.dismiss();
            }
        });

    }

    private void displaySetClassNumberDialog(final int indexClassNum) {

        classNumberSelectDialog = new ProductionManagementCustomSelectClassNumberDialog(
                ctx);
        classNumberSelectDialog.show();

        TextView v1 = (TextView) classNumberSelectDialog.findViewById(R.id.v1);
        TextView v2 = (TextView) classNumberSelectDialog.findViewById(R.id.v2);
        TextView v3 = (TextView) classNumberSelectDialog.findViewById(R.id.v3);
        TextView v4 = (TextView) classNumberSelectDialog.findViewById(R.id.v4);
        TextView v5 = (TextView) classNumberSelectDialog.findViewById(R.id.v5);

        String[] content = {"1", "2", "3", "4", "0"};
        v1.setText(content[0]);
        v2.setText(content[1]);
        v3.setText(content[2]);
        v4.setText(content[3]);
        v5.setText(content[4]);

        v1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_class[indexClassNum].getText().toString().equals("0")) {
                    rl_start_time_select[indexClassNum].setEnabled(true);
                    tv_start_time[indexClassNum].setText("08:00");
                }
                tv_class[indexClassNum].setText("1");
                classNumberSelectDialog.dismiss();
            }
        });
        v2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_class[indexClassNum].getText().toString().equals("0")) {
                    rl_start_time_select[indexClassNum].setEnabled(true);
                    tv_start_time[indexClassNum].setText("14:00");
                }
                tv_class[indexClassNum].setText("2");
                classNumberSelectDialog.dismiss();
            }
        });
        v3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_class[indexClassNum].getText().toString().equals("0")) {
                    rl_start_time_select[indexClassNum].setEnabled(true);
                    tv_start_time[indexClassNum].setText("20:00");
                }
                tv_class[indexClassNum].setText("3");
                classNumberSelectDialog.dismiss();
            }
        });
        v4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_class[indexClassNum].getText().toString().equals("0")) {
                    rl_start_time_select[indexClassNum].setEnabled(true);
                    tv_start_time[indexClassNum].setText("02:00");
                }
                tv_class[indexClassNum].setText("4");
                classNumberSelectDialog.dismiss();
            }
        });
        v5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_class[indexClassNum].setText("0");
                tv_start_time[indexClassNum].setText("--:--");
                rl_start_time_select[indexClassNum].setEnabled(false);
                classNumberSelectDialog.dismiss();
            }
        });
    }

}
